package com.eassy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eassy.bean.Member;
import com.eassy.config.Result;
import com.eassy.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("member")
@CrossOrigin
public class MemberController {
    @Autowired
    MemberService memberService;

    @GetMapping("list")
    public Result getList() {
        List<Member> list = memberService.list();
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Member member){
        memberService.save(member);
        Member memberSerivceById= memberService.getById(member.getId());
        return Result.success("保存成功",memberSerivceById);
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") int id){
        boolean b= memberService.removeById(id);
        if(b){
            return Result.success("删除成功");
        }else{
            return Result.fail(201,"删除失败");
        }
    }

    @PutMapping("update")
    public  Result edit(@RequestBody Member member){
        boolean b = memberService.updateById(member);
        Member member1=memberService.getById(member.getId());
        if(b){
            return Result.success("修改成功");
        }else{
            return Result.fail(201,"修改失败");
        }
    }
    @GetMapping("page")
    public Result getPage(Page page){
        Page result = memberService.page(page);
        return Result.success(result);
    }
}